package com.jinlejia.bbs.ui.reply;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.czt.mp3recorder.MP3Recorder;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * @author 王进 on 2019/4/8.
 */

public class RecorderHelper {
    private File cacheDir;
    private RecorderHelper(){
    }

    public void init(Context context){
        this.cacheDir = context.getCacheDir();
    }

    private volatile static RecorderHelper instance;
    public static RecorderHelper instance() {
        if (instance == null) {
            synchronized (RecorderHelper.class) {
                if (instance == null) {
                    instance = new RecorderHelper();
                }
            }
        }
        return instance;
    }
    private boolean isRun = false;

    private MP3Recorder recorder;
    private RecorderListener recorderListener;
    private Handler mainHandler;
    public void setRecorderListener(RecorderListener recorderListener) {
        this.recorderListener = recorderListener;
    }

    private class MainHandler extends Handler{
        MainHandler(){
            super(Looper.getMainLooper());
        }

        @Override
        public void handleMessage(Message msg) {
            if (recorderListener!=null){
                if (msg.arg2!=0){
                    stop(true);
                }else {
                    recorderListener.onVolume(msg.arg1);
                }
            }

        }
    }
    private File mp3File;
    public void start(){
        if (isRun)return;
        mainHandler = new MainHandler();
        isRun = true;
        String cacheDirPath = this.cacheDir.getPath() ;
        String path = cacheDirPath + File.separator  + getStringDateShort() + ".mp3";
        mp3File = new File(path);
        recorder = new MP3Recorder(mp3File);
        try {
            recorder.start();
            if (recorderListener!=null){
                recorderListener.onStart();
            }
            new Thread(new Runnable() {
                private long time;
                private long beginTime;
                @Override
                public void run() {
                    while (isRun){
                        if (recorderListener!=null){
                            int volume = 0;
                            if (volume!=recorder.getVolume()){
                                volume = recorder.getVolume();
                                long space = System.currentTimeMillis() - time;
                                if (space>=500){
                                    time = System.currentTimeMillis();
                                    Message msg = Message.obtain();
                                    msg.arg1 = volume;
                                    mainHandler.sendMessage(msg);
                                }
                            }
                            if (beginTime==0){
                                beginTime = System.currentTimeMillis();
                            }
                            long five = System.currentTimeMillis() - beginTime;
                            if (five>= TimeUnit.MINUTES.toMillis(5)){ //如果是五分钟，则停止
                                Message msg = Message.obtain();
                                msg.arg2 = 1;
                                mainHandler.sendMessage(msg);
                            }
//                            if (five>= TimeUnit.SECONDS.toMillis(10)){ //测试，最多上传10秒
//                                Message msg = Message.obtain();
//                                msg.arg2 = 1;
//                                mainHandler.sendMessage(msg);
//                            }
                        }
                    }
                }
            }).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String getStringDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
        return formatter.format(currentTime);
    }

    public interface RecorderListener{
        void onStart();
        void onStop(File file,boolean isMax);
        void onVolume(int volume);
    }

    public void stop(final boolean isMax){
        isRun = false;
        if (recorderListener!=null){
            recorder.stop();
            mainHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    recorderListener.onStop(mp3File,isMax);
                }
            },500);

        }
    }
}
